package org.liziyi.pss.model.queue;

import org.liziyi.pss.entity.ProcessControlBlock;
import org.liziyi.pss.common.enums.QueueEnum;
import org.liziyi.pss.gui.QueueGroupGUI;
import org.liziyi.pss.model.ScheduleQueue;
import java.util.Map;

/**
 * @author liziyi
 */
public class ScheduleQueueGroup {
    private final Map<QueueEnum, ScheduleQueue<ProcessControlBlock>> queueMap;
    private final QueueGroupGUI queueGroupGUI;

    public ScheduleQueueGroup(QueueGroupGUI queueGroupGUI,Map<QueueEnum, ScheduleQueue<ProcessControlBlock>> queueMap) {
        this.queueMap = queueMap;
        this.queueGroupGUI = queueGroupGUI;
    }

    /**
     * 向队列组中某个队列添加元素
     * @param queueEnum
     * @param pcb
     */
    public void addElement(QueueEnum queueEnum, ProcessControlBlock pcb) {
        queueMap.get(queueEnum).add(pcb);
        if(queueEnum==QueueEnum.FINISH_QUEUE){
            ((BackUpQueue)queueMap.get(QueueEnum.BACKUP_QUEUE)).addFinishedPid(pcb.getPid());
        }
    }

    /**
     * 从队列组中某个队列弹出元素
     * @param queueEnum
     * @return
     */
    public ProcessControlBlock pollElement(QueueEnum queueEnum) {
        return queueMap.get(queueEnum).poll();
    }

    public ProcessControlBlock removeElement(QueueEnum queueEnum,int pid){
        return queueMap.get(queueEnum).remove(pid);
    }
    public QueueGroupGUI getQueueGroupGUI() {
        return queueGroupGUI;
    }

    public boolean isEmpty(QueueEnum queueEnum){
        return queueMap.get(queueEnum).isEmpty();
    }
    public void reset(){
        queueMap.forEach((queueEnum, scheduleQueue) -> {
            scheduleQueue.reset();
        });
    }
}
